package com.example.houtai.service.impl;

import com.example.houtai.entity.EsSave;
import com.example.houtai.entity.SysRegion;
import com.example.houtai.mapper.*;
import com.example.houtai.service.EsService;
import com.example.houtai.utils.EsUtils;
import com.org.dto.EsSaveDto;
import com.org.dto.IdNamePair;
import com.org.vo.ResultEntity;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class EsServiceImpl implements EsService {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Autowired
    private TbPremisesInfoMapper tbPremisesInfoMapper;

    @Autowired
    private TbRoomInfoMapper tbRoomInfoMapper;

    @Autowired
    private TbHouseInfoMapper tbHouseInfoMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private EsMapper esMapper;

    EsSave esSave;

    /**
     * ES全量
     *
     * @return
     */
    @SneakyThrows
    @Override
    public ResultEntity save() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startTime = new Date();
        log.info("ES全量同步开始：" + dateFormat.format(startTime));
        //干掉原来的
        /*boolean essave = elasticsearchTemplate.deleteIndex("essave");
        if (!essave) {
            log.error("清空索引失败");
            return ResultEntity.ok("400", "全量同步失败", null);
        }*/
        log.info("清空索引成功");
        try {
            List<EsSaveDto> esSaveList = esMapper.esSavelistAll();
            for (EsSaveDto esSaveDto : esSaveList) {
                EsSave esSave = new EsSave();
                //省
                List<SysRegion> sysRegions = esMapper.SelProvice(esSaveDto.getProvince());
                for (SysRegion sysRegion : sysRegions) {
                    esSaveDto.setProvinceId(sysRegion.getRegionId());
                    esSaveDto.setProvinceName(sysRegion.getRegionName());
                    esSaveDto.setProvincePinyin(sysRegion.getRegionNameEn());
                    esSaveDto.setProvincePinyinShort(sysRegion.getRegionShortnameEn());
                }
                //市
                List<SysRegion> sysRegions1 = esMapper.SelCity(esSaveDto.getCity());
                for (SysRegion sysRegion : sysRegions1) {
                    esSaveDto.setCityId(sysRegion.getRegionId());
                    esSaveDto.setCityName(sysRegion.getRegionName());
                    esSaveDto.setCityPinyin(sysRegion.getRegionNameEn());
                    esSaveDto.setCityPinyinShort(sysRegion.getRegionShortnameEn());
                }
                //区县
                List<SysRegion> sysRegions2 = esMapper.SelDistrict(esSaveDto.getCounty());
                for (SysRegion region : sysRegions2) {
                    esSaveDto.setDistrictId(region.getRegionId());
                    esSaveDto.setDistrictName(region.getRegionName());
                    esSaveDto.setDistrictPinyin(region.getRegionNameEn());
                    esSaveDto.setDistrictPinyinShort(region.getRegionShortnameEn());
                }

                List<IdNamePair> businessCircleList = esMapper.businessCircleList(esSaveDto.getCounty());
                if (businessCircleList.size() > 0) {
                    esSaveDto.setBusinessCircleList(businessCircleList);
                }
                List<IdNamePair> subwayList = esMapper.subwayList(esSaveDto.getCity());
                if (subwayList.size() > 0) {
                    esSaveDto.setSubwayList(subwayList);
                }
                BeanUtils.copyProperties(esSaveDto, esSave);
                esSave.setManagerInfo(esMapper.SelManagerInfo(esSaveDto.getManager()));
                EsUtils.save(elasticsearchTemplate, esSave, esSave.getId());

                log.info("-----ES同步中-----" + esSave);
            }
            Date endTime = new Date();
            log.info("ES全量同步结束：" + dateFormat.format(endTime));
            log.info("共用时：" + (endTime.getTime() - startTime.getTime()) / 1000 + "秒");
            esMapper.updSyncEsSave();
            return ResultEntity.ok("200", "全量同步成功", esSaveList);
        } catch (Exception e) {
            log.info("ES全量同步失败：" + String.valueOf(e));
            return ResultEntity.ok("400", "全量同步失败", null);
        }
    }

    @Override
    public List<IdNamePair> businessCircleList(Integer cid) {
        return esMapper.businessCircleList(cid);
    }

    @Override
    public List<IdNamePair> subwayList(Integer did) {
        return esMapper.subwayList(did);
    }
}
